package com.abou.connector.exception;

import java.io.Serializable;
import org.apache.commons.lang.StringUtils;
import com.abou.utils.common.BeanUtils;

/**
 * An unchecked exception wrapping all sub system exceptions.
 * 
 * @author ztrt8431
 */
public class ConnectorException extends RuntimeException implements
    Serializable {
  
  /**
   * Serial version uid for the class
   */
  private static final long serialVersionUID = 1L;
  
  private Object code;
  private Object[] params = new Object[0];
  
  /**
   * The constructor
   * 
   * @param wrappedEx The exception to wrap
   */
  public ConnectorException(Exception wrappedEx) {

    super(wrappedEx);
    
    // building the system message
    String message = wrappedEx.getLocalizedMessage();
    
    if (StringUtils.isBlank(message)) {
      
      message = wrappedEx.getMessage();
    }
    
    params = new String[] { message };
  }
  
  /**
   * The constructor with an error code
   * 
   * @param code See {@link #getCode()}.
   */
  public ConnectorException(Object code) {

    this.code = code;
  }
  
  /**
   * The constructor with an error code and parameters for the message
   * 
   * @param code See {@link #getCode()}.
   * @param params See {@link #getParams()}.
   * @param wrappedEx The exception to wrap
   */
  public ConnectorException(Object code, Object[] params, Exception wrappedEx) {

    super(wrappedEx);
    this.code = code;
    
    if (params != null) {
      
      this.params = BeanUtils.clone(params);
    }
  }
  
  /**
   * @return The code for the exception, as returned by the web service
   */
  public Object getCode() {

    return code;
  }
  
  /**
   * @return The parameters for the displayable exception message
   */
  public Object[] getParams() {

    return params;
  }
  
  @Override
  public String toString() {

    return super.toString() + "\nCode : " + getCode();
  }
}
